package objects;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Line;

import states.SlickGame;
import globals.Vector2D;

public class Runner {
	public float x,y,r,speed;			// Core data
	public Vector2D direction;
	public float goToX,goToY;
	public boolean changeDirection=false;
	
	private int curTime; // Wave Effect Data
	private ArrayList<Wave> waveList;
	public Color colorMe,colorWave;
	// default 30
	public static int rateWaveSpawn=30;	// global effects
	public static float rateWaveSpread=(float)(1.12); // default 1.12
	public static int maxWaveRadius=20;
	public static boolean debug=false;
	public static boolean invisible=false;
	public static boolean followAlways=false; // turn this to true if you like interesting things.
	public static boolean leaderWave=false;
	
	public Runner(){
		x=0;	goToX=0;
		y=0;	goToY=0;
		r=1;	speed=0;
		direction=new Vector2D();
		curTime=0;
		colorMe=Color.black;
		colorWave=Color.black;
		waveList=new ArrayList<Wave>();
	}
	public Runner(int xin, int yin, int speedIn){
		x=xin;	goToX=0;
		y=yin;	goToY=0;
		r=3;	speed=speedIn;
		direction=new Vector2D(SlickGame.width/2,SlickGame.height/2);
		direction.setMagnatude(speed);
		curTime=0;
		colorMe=Color.blue.brighter();
		colorWave=Color.blue;
		waveList=new ArrayList<Wave>();
	}
	public Runner(int xin, int yin, int rin, int speedIn, Color me, Color wave){
		x=xin;	goToX=0;
		y=yin;	goToY=0;
		r=rin;	speed=speedIn;
		direction=new Vector2D(SlickGame.width/2,SlickGame.height/2);
		direction.setMagnatude(speed);
		curTime=0;
		colorMe=me;
		colorWave=wave;
		waveList=new ArrayList<Wave>();
	}
	
	public void update(int delta,Runner leader){
		curTime += delta;
		if(curTime>rateWaveSpawn){
			curTime=0;
			waveList.add(new CircleWave((int)x, (int)y, rateWaveSpread, colorWave));
		}
		
		for(int i=0; i<waveList.size();i++){
			waveList.get(i).update();
			if(waveList.get(i).getRadius()>maxWaveRadius){
				waveList.remove(i);
				i--;
			}
		}
		
		if(changeDirection){
			goToX=leader.x;
			goToY=leader.y;
			direction.setX(goToX);
			direction.setY(goToY);
			direction.setMagnatude(speed);
			changeDirection=false||followAlways; 
		}
		
		if(x<-r){x=SlickGame.width+r; changeDirection=true;}
		else if(x>SlickGame.width+r){x=-r; changeDirection=true;}
		
		if(y<-r){y=SlickGame.height+r; changeDirection=true;}
		else if(y>SlickGame.height+r){y=r; changeDirection=true;}
		
		x+=direction.getX();
		y+=direction.getY();
	}
	public void update(int delta){
		curTime += delta;
		if(curTime>rateWaveSpawn){
			curTime=0;
			waveList.add(new CircleWave((int)x, (int)y, rateWaveSpread, colorWave));
		}
		
		for(int i=0; i<waveList.size();i++){
			waveList.get(i).update();
			if(waveList.get(i).getRadius()>maxWaveRadius){
				waveList.remove(i);
				i--;
			}
		}
		
		if(changeDirection){
			goToX=(float) (((SlickGame.width-20)*Math.random())+10);
			goToY=(float) (((SlickGame.height-20)*Math.random())+10);
			direction.setX(goToX);
			direction.setY(goToY);
			
			if(leaderWave){
				goToX=(float) (SlickGame.width-20);
				goToY=(float) (((SlickGame.height-100)*Math.random())+75);
				direction.setX(goToX);
				direction.setY(goToY);
			}
			
			//direction.setPoint(goToX, goToY);
			direction.setMagnatude(speed);
			changeDirection=false;
		}
		
		if(x<-r){x=SlickGame.width+r; changeDirection=true;}
		else if(x>SlickGame.width+r){x=-r; changeDirection=true;}
		
		if(y<-r){y=SlickGame.height+r; changeDirection=true;}
		else if(y>SlickGame.height+r){y=-r; changeDirection=true;}
		
		x+=direction.getX();
		y+=direction.getY();
	}
	
	public void draw(Graphics g){
		if(!waveList.isEmpty()){
			for(int i=0; i<waveList.size();i++){
				waveList.get(i).draw(g);
			}
		}
		if(!invisible){
			g.setColor(colorMe);
			g.draw(new Circle(x,y,r));
		}
		
		if(debug){
			g.setColor(colorMe.multiply(Color.white));
			g.draw(new Circle(goToX,goToY,3));
			g.draw(new Circle(goToX,goToY,5));
			Vector2D temp=this.direction.getCopy();
			g.draw(new Line(x,y,(x+(9*temp.getX())),(y+(9*temp.getY()))));
		}
		
	}
}
